;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.ProcurementTimePeriod", [])
        .controller("ProcurementTimePeriodCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            // 分页信息
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 标签页信息
            $scope.activeFlag = [true, false];

            // 高级搜索配置
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.tbConf = {
                scrollWidth: 1200,
                tableCol: [
                    {
                        name: "代理商",
                        dataField: "purchaseOrgName",
                        width: 140,
                        value: '',
                        isShow: true,
                        isFixed: true
                    },
                    {
                        name: "1月",
                        dataField: "oneMouthAmount",
                        width: 80,
                        value: 1,
                        isShow: true,
                    },
                    {
                        name: "2月",
                        dataField: "towMouthAmount",
                        width: 80,
                        value: 2,
                        isShow: true,
                    },
                    {
                        name: "3月",
                        dataField: "thereMouthAmount",
                        width: 80,
                        value: 3,
                        isShow: true,
                    },
                    {
                        name: "4月",
                        dataField: "fourMouthAmount",
                        width: 80,
                        value: 4,
                        isShow: true,
                    },
                    {
                        name: "5月",
                        dataField: "fiveMouthAmount",
                        width: 80,
                        value: 5,
                        isShow: true,
                    },
                    {
                        name: "6月",
                        dataField: "sixMouthAmount",
                        width: 80,
                        value: 6,
                        isShow: true,
                    },
                    {
                        name: "7月",
                        dataField: "sevenMouthAmount",
                        width: 80,
                        value: 7,
                        isShow: true,
                    },
                    {
                        name: "8月",
                        dataField: "eigthMouthAmount",
                        width: 80,
                        value: 8,
                        isShow: true,
                    },
                    {
                        name: "9月",
                        dataField: "nineMouthAmount",
                        width: 80,
                        value: 9,
                        isShow: true,
                    },
                    {
                        name: "10月",
                        dataField: "tenMouthAmount",
                        width: 80,
                        value: 10,
                        isShow: true,
                    },
                    {
                        name: "11月",
                        dataField: "elevenMouthAmount",
                        width: 80,
                        value: 11,
                        isShow: true,
                    },
                    {
                        name: "12月",
                        dataField: "twelveMouthAmount",
                        width: 80,
                        value: 12,
                        isShow: true,
                    },
                    {
                        name: "小计",
                        dataField: "subtotal",
                        width: 100,
                        value: '',
                        isShow: true,
                        isFixed: true
                    },
                ]
            };

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/report/exportTimePeriodPurchaseSituation"),
            };
            var now = new Date();
            // 搜索条件
            $scope.condition = {
                date: now
            };
            // 所有月份信息
            $scope.monthData = [
                {name: '1月', value: 1, isChecked: true, isSearch: true}, {name: '2月', value: 2, isChecked: true, isSearch: true},
                {name: '3月', value: 3, isChecked: true, isSearch: true}, {name: '4月', value: 4, isChecked: true, isSearch: true},
                {name: '5月', value: 5, isChecked: true, isSearch: true}, {name: '6月', value: 6, isChecked: true, isSearch: true},
                {name: '7月', value: 7, isChecked: true, isSearch: true}, {name: '8月', value: 8, isChecked: true, isSearch: true},
                {name: '9月', value: 9, isChecked: true, isSearch: true}, {name: '10月',value: 10, isChecked: true, isSearch: true},
                {name: '11月', value: 11, isChecked: true, isSearch: true}, {name: '12月', value: 12, isChecked: true, isSearch: true}
            ];

            // 时间选择器配置
            $scope.dateOptions = {
                minMode:'year',
                datepickerMode: 'year'
            };

            $scope.checkedMonth = '';        // 已经选择的月份字符串

            $scope.dataList = [];
            $scope.totalRow = [];            // 列表的总计行

            // 选择月份筛选条件
            $scope.selectMonth = function () {
                var checkedMonth = [];          // 已选中的月份
                var monthCount = [];            // 调接口筛选的参数
                angular.forEach($scope.monthData, function (item) {
                    if (item.isChecked) {
                        checkedMonth.push(item.name);
                        monthCount.push(item.value);
                    }
                });
                $scope.condition.monthCount = monthCount;
                $scope.checkedMonth = checkedMonth.join(',');
            };

            // 高级搜索确认搜索
            $scope.confirmSearch = function () {
                angular.forEach($scope.tbConf.tableCol, function (item) {
                    if ($scope.condition.monthCount.indexOf(item.value) < 0 && !item.isFixed) {
                        item.isShow = false;
                    } else {
                        item.isShow = true;
                    }
                });
                $scope.searchData(1);
            };

            // 切换高级搜索
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            //重置搜索条件
            $scope.reset = function () {
                $scope.condition = { date: now };
                $scope.checkedMonth = '';
                angular.forEach($scope.monthData, function (item) {
                    item.isChecked = true;
                });
                angular.forEach($scope.tbConf.tableCol, function (item) {
                    item.isShow = true;
                });
                $scope.searchData(1);
            };

            // 按钮权限code
            var btnPermissionCode = {
                procurementTimePeriodExport: window.ActionCode.procurementTimePeriodExport
            };

            // 按钮权限
            $scope.btnPermission = {
                procurementTimePeriodExport: true
            };

            // 设置按钮权限
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            // 获取报表列表
            $scope.searchData = function (curPage) {
                if (!$scope.condition.date) {
                    ybWidget.ShowMessage('年份不能为空，请选择年份！', MESG_TYPE.Error);
                    return;
                }
                var url = '/report/timePeriodPurchaseSituation';
                if (curPage) $scope.pageInfo.curPage = curPage;
                var params = {
                    filters: angular.copy($scope.condition),
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.totalRow = res.value.list.splice(0, 1);
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                })
            };

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })()
        }])
    ;
})(window, window.angular);